package elijah.base.dialog;

import android.content.Context;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.core.FullScreenDialog;
import com.lxj.xpopup.impl.FullScreenPopupView;
import com.lxj.xpopup.interfaces.XPopupCallback;

import elijah.base.utils.ClassUtils;
import elijah.base.utils.DensityUtils;


public abstract class FullScreenDialogView<V extends ViewBinding> extends FullScreenPopupView {

    protected BasePopupView dialog;
    private XPopupCallback popupCallback;
    private boolean dismissOnTouchOutside = false;
    protected V binding;


    protected void addInnerContent() {

        binding = getViewBinding();
        contentView = binding.getRoot();
        LayoutParams params = (LayoutParams) contentView.getLayoutParams();
        params.gravity = Gravity.CENTER;
        fullPopupContainer.addView(contentView, params);
    }

    protected V getViewBinding() {
        return (V) ClassUtils.getViewBinding(getClass(), 0, fullPopupContainer);
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();
    }

    protected abstract void initView();

    public FullScreenDialogView<V> setPopupCallback(XPopupCallback popupCallback) {
        this.popupCallback = popupCallback;
        return this;
    }

    public FullScreenDialogView(@NonNull Context context) {
        super(context);
    }


    public FullScreenDialogView<V> show() {
        showDialog(this);
        return this;
    }


    public FullScreenDialogView<V> setDismissOnTouchOutside(boolean dismissOnTouchOutside) {
        this.dismissOnTouchOutside = dismissOnTouchOutside;
        return this;
    }


    protected XPopup.Builder dialogBuilder(){
        return new XPopup.Builder(getContext());
    }

    protected void showDialog(BasePopupView view) {
        XPopup.Builder builder = dialogBuilder();

        dialog = builder
                .autoOpenSoftInput(false)
                .autoDismiss(false)
                .dismissOnTouchOutside(dismissOnTouchOutside)
                .setPopupCallback(popupCallback == null ? new BasePopupCallback() {
                } : popupCallback)
                .hasNavigationBar(false)
                .hasStatusBar(false)
                .asCustom(view)
                .show();


    }


}
